package com.example.myapplication.HomeRecyclerViewAdapter;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.example.myapplication.HomeRecyclerViewFragment.HomeRecyclerViewBean.JingPinBanJiBean;
import com.example.myapplication.R;

import java.util.ArrayList;
import java.util.List;

public class JingPinBanJiAdapter extends RecyclerView.Adapter<JingPinBanJiAdapter.ViewHolder> {
    List<JingPinBanJiBean>list=new ArrayList<>();
    //定义一个静态类使其继承RecyclerView.ViewHolder而静态类的名字就是上面
    //RecyclerView.Adapter<JingPinBanJiAdapter.ViewHolder> 中.ViewHolder部分
    //在静态类中定义和绑定数据
    static class ViewHolder extends RecyclerView.ViewHolder{
        View view;
        ImageView img;
        TextView name;
        public ViewHolder(@NonNull View itemView) {
            super(itemView);
            view=itemView;
            img=view.findViewById(R.id.JingPinBanJi_MoKuai_img);
            name=view.findViewById(R.id.JingPinBanJi_MoKuai_name);
        }

    }

    public JingPinBanJiAdapter(List<JingPinBanJiBean> list) {
        this.list = list;
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view= LayoutInflater.from(parent.getContext()).inflate(R.layout.jingpinbanji_mokuai,parent,false);
        //创建一个ViewHolder绑定解析到的View
        final ViewHolder viewHolder=new ViewHolder(view);
        //点击事件
        viewHolder.img.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                int position=viewHolder.getAdapterPosition();
                //定义Bean数据用于获取所点击的索引
                JingPinBanJiBean jingPinBanJiBean=list.get(position);
                Toast.makeText(view.getContext(), jingPinBanJiBean.getName(), Toast.LENGTH_SHORT).show();
            }
        });
        return viewHolder;
    }

    @Override
    public void onBindViewHolder(@NonNull JingPinBanJiAdapter.ViewHolder holder, int position) {
        JingPinBanJiBean jingPinBanJiBean=list.get(position);
        holder.img.setImageResource(list.get(position).getImg());
        holder.name.setText(list.get(position).getName());
    }

    @Override
    public int getItemCount() {
        return list.size();
    }
}
